package red.dao.impl;

import red.dao.ArticleDao;
import red.model.ArticleModel;
import red.util.MysqlConnectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@SuppressWarnings("ALL")
public class ArticleDaoImpl implements ArticleDao {
    MysqlConnectUtils mysqlConnectUtils=new MysqlConnectUtils();
    @Override
    public ArticleModel addarticle(ArticleModel articleModel) {
        String article_id = articleModel.getArticle_id();
        String headline = articleModel.getHeadline();
        String content=articleModel.getContent();
        String user_name=articleModel.getUser_name();
        String page=articleModel.getPage();
        String sql = "insert into article(article_id,user_name,headline,content,page) "
                + "value('"+article_id+"','"+user_name+"','"+headline+"','"+content+"','"+page+"')";
        mysqlConnectUtils.insert(sql);
        return articleModel;
    }


    // 删除文章标题
    @Override
    public ArticleModel deleteArticle(ArticleModel articleModel) {
        String headline = articleModel.getHeadline();
        String sql = "delete from article where headline= "
                + "'"+headline+ "'";
        mysqlConnectUtils.delete(sql);
        return articleModel;
    }

    // 查询文章<标题查询><栏目><人名>
    @Override
    public List<ArticleModel> selectArticle(ArticleModel articleModel) {
        String headline = articleModel.getHeadline();
        String sql = "select * from article where headline= "
                + "'"+headline+ "'";
        List<ArticleModel> models =new ArrayList<>();
        List<Map<String, Object>> list = mysqlConnectUtils.list(sql);
        for(int i=0;i<list.size();i++){
            Map<String, Object> row = list.get(i);
            ArticleModel model = new ArticleModel();
            model.setArticle_id(row.get("article_id").toString());
            model.setUser_name(row.get("user_name").toString());
            model.setHeadline(row.get("headline").toString());
            model.setColumn_name(row.get("column_name").toString());
            model.setContent(row.get("content").toString());
            if (row.get("page")!=null){
                model.setPage(row.get("page").toString());
            }
            if (row.get("like_easy")!=null){
                model.setLike_easy(row.get("like_easy").toString());
            }
            if (row.get("like_number")!=null){
                model.setLike_number(row.get("like_number").toString());
            }
            model.setAudit(row.get("audit").toString());
            model.setPublish_date(row.get("publish_date").toString());
            model.setColumn_id(row.get("column_id").toString());
            model.setUser_id(row.get("user_id").toString());
            models.add(model);
        }
        return models;
    }

    @Override
    public List<ArticleModel> selectArticleColumn(ArticleModel articleModel) {
        String column_name = articleModel.getColumn_name();
        String sql = "select * from article where column_name= "
                + "'"+column_name+ "'";
        List<ArticleModel> models =new ArrayList<>();
        List<Map<String, Object>> list = mysqlConnectUtils.list(sql);
        for(int i=0;i<list.size();i++){
            Map<String, Object> row = list.get(i);
            ArticleModel model = new ArticleModel();
            model.setArticle_id(row.get("article_id").toString());
            model.setUser_name(row.get("user_name").toString());
            model.setHeadline(row.get("headline").toString());
            model.setColumn_name(row.get("column_name").toString());
            model.setContent(row.get("content").toString());
            if (row.get("page")!=null){
                model.setPage(row.get("page").toString());
            }
            if (row.get("like_easy")!=null){
                model.setLike_easy(row.get("like_easy").toString());
            }
            if (row.get("like_number")!=null){
                model.setLike_number(row.get("like_number").toString());
            }
            model.setAudit(row.get("audit").toString());
            model.setPublish_date(row.get("publish_date").toString());
            model.setColumn_id(row.get("column_id").toString());
            model.setUser_id(row.get("user_id").toString());
            models.add(model);
        }
        return models;
    }

    @Override
    public List<ArticleModel> selectArticleUser(ArticleModel articleModel) {
        String user_name = articleModel.getUser_name();
        String sql = "select * from article where user_name= "
                + "'"+user_name+ "'";
        List<ArticleModel> models =new ArrayList<>();
        List<Map<String, Object>> list = mysqlConnectUtils.list(sql);
        for(int i=0;i<list.size();i++){
            Map<String, Object> row = list.get(i);
            ArticleModel model = new ArticleModel();
            model.setArticle_id(row.get("article_id").toString());
            model.setUser_name(row.get("user_name").toString());
            model.setHeadline(row.get("headline").toString());
            model.setColumn_name(row.get("column_name").toString());
            model.setContent(row.get("content").toString());
            if (row.get("page")!=null){
                model.setPage(row.get("page").toString());
            }
            if (row.get("like_easy")!=null){
                model.setLike_easy(row.get("like_easy").toString());
            }
            if (row.get("like_number")!=null){
                model.setLike_number(row.get("like_number").toString());
            }
            model.setAudit(row.get("audit").toString());
            model.setPublish_date(row.get("publish_date").toString());
            model.setColumn_id(row.get("column_id").toString());
            model.setUser_id(row.get("user_id").toString());
            models.add(model);
        }
        return models;
    }

    //查询所有文章
    @Override
    public List<ArticleModel> selectq(ArticleModel articleModel) {
        String sql = "select * from article ";
        List<ArticleModel> models =new ArrayList<>();
        List<Map<String, Object>> list = mysqlConnectUtils.list(sql);
        for(int i=0;i<list.size();i++){
            Map<String, Object> row = list.get(i);
            ArticleModel model = new ArticleModel();
            model.setArticle_id(row.get("article_id").toString());
            model.setUser_name(row.get("user_name").toString());
            model.setHeadline(row.get("headline").toString());
            model.setColumn_name(row.get("column_name").toString());
            model.setContent(row.get("content").toString());
            if (row.get("page")!=null){
                model.setPage(row.get("page").toString());
            }
            if (row.get("like_easy")!=null){
                model.setLike_easy(row.get("like_easy").toString());
            }
            if (row.get("like_number")!=null){
                model.setLike_number(row.get("like_number").toString());
            }
            model.setAudit(row.get("audit").toString());
            model.setPublish_date(row.get("publish_date").toString());
            model.setColumn_id(row.get("column_id").toString());
            model.setUser_id(row.get("user_id").toString());
            models.add(model);
        }
        return models;
    }


}